<template>
   <div class="login-container">
    <van-nav-bar title="登录">
      <template #left>
        <img class="logo" src="@/assets/images/logo.png"/>
      </template>
    </van-nav-bar>
     <van-form @submit="onSubmit">
      <van-field
        v-model="loginForm.mobile"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[{ required: true, message: '请填写手机号', trigger: 'onChange', pattern: /^1[3456789]\d{9}$/  }]"
      />
      <van-field
        v-model="loginForm.code"
        type="password"
        label="密码"
        placeholder="请输入密码"
        required
        :rules="[{ required: true, message: '请填写密码', trigger: 'onChange', pattern: /^\d{6}$/ }]"
      />
      <div style="margin: 16px;">
        <van-button block type="info" native-type="submit"    :loading='isloading'  loading-text="加载中..."          >登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
// import { mapMutations } from 'vuex'

import { setToken, setRefreshToken } from '@/utils/token'

import { loginAPI } from '@/api/login'
export default {
  name: 'Login',
  data () {
    return {
      isloading: false, // 加载状态
      loginForm: {
        mobile: '13888888888',
        code: '246810' // 默认只能用246810后台规定了
      }
    }
  },
  methods: {

    // ...mapMutations('user', ['setToken', 'setRefreshToken']),

    // 登录提交时处发 撒泼米特 事件
    async onSubmit () {
      try {
        this.isloading = true
        const resp = await loginAPI(this.loginForm)

        const { data: { refresh_token, token } } = resp
        setToken(token)

        setRefreshToken(refresh_token)

        this.$notify({ type: 'success', message: '登录成功' })

        this.$router.replace(this.$route.query.redirect || '/')
      } catch (e) {
        this.$notify({ type: 'warning', message: '手机号或密码错误' })
      } finally {
        this.isloading = false
      }
    }
  }

}
</script>

<style  scoped lang="less"  >
.logo {
  width: 100px;
  height: 30px;
}

//  .van-nav-bar {
//    background: #007bff;
// /deep/ .van-nav-bar__title{color: white;
// }

// }

</style>
